package com.tripreset.sdk

import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabsIntent
import com.blankj.utilcode.util.LogUtils
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import com.tripreset.sdk.bk.BaiDuDataSource
import com.tripreset.sdk.bk.CommonDataSource
import com.tripreset.sdk.bk.SouGouDataSource
import com.tripreset.sdk.bk.TouTiaoDataSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MainActivity : AppCompatActivity() {

    companion object {
        fun init() {
            val map = HashMap<String, Any>()
            map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
            map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
            QbSdk.initTbsSettings(map)

        }
    }

    fun hasAd(url: String): Boolean {
        val adUrls: Array<String> = resources.getStringArray(R.array.adBlockUrl)
        for (adUrl in adUrls) {
            if (url.contains(adUrl)) {
                return true
            }
        }
        return false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
        setContentView(R.layout.activity_main)

        val touTiaoDataSource = TouTiaoDataSource()
        val dataSource = touTiaoDataSource.getDataSourceUrl("都江堰")
        val baiDuDataSource = BaiDuDataSource()
        val dataSource2 = baiDuDataSource.getDataSourceUrl("都江堰")
        val souGouDataSource = SouGouDataSource()
        val dataSource3 = souGouDataSource.getDataSourceUrl("都江堰")

        //getHtml(baiDuDataSource, findViewById(R.id.content2))
        //getHtml(touTiaoDataSource, findViewById(R.id.content1))
        val dataSource1 = CommonDataSource()


        findViewById<View>(R.id.baidu).setOnClickListener {
            GlobalScope.launch {
                val result = withContext(Dispatchers.IO) {
                    dataSource1.loadContent("https://s.ereuiib.com/E1.P4oP2H", true)
                }
                LogUtils.i("=====================$result")
            }
        }
        findViewById<View>(R.id.toutiao).setOnClickListener {
            MainActivity2.start(this, dataSource)
        }
        findViewById<View>(R.id.sougou).setOnClickListener {
            GlobalScope.launch {
                val result = withContext(Dispatchers.IO) {
                    dataSource1.setUserAgent("")
                    dataSource1.loadContent("https://m.ly.com/go/youji/view/youjiMain.html?youjiId=1128396&youjicode=21012721505591091625&from=tuijian#/travelNotes",true)
                }
                LogUtils.i("=====================$result")
            }
        }



        findViewById<View>(R.id.content3).setOnClickListener {

            //if (activitiesList.size > 0) {
            // 使用Chrome Custom Tab打开
            val url = "https://news.163.com/21/0106/12/FVLIJBSH000189FH.html?clickfrom=w_yw"
            val builder = CustomTabsIntent.Builder()
            val customTabsIntent = builder.build()
            //customTabsIntent.launchUrl(this, Uri.parse(url))


            //}
        }
    }
}